package cn.ds.assets.module.assets.dal.mysql.dataQualityRule;

import java.util.*;
import java.time.LocalDateTime;
import java.time.LocalDate;

import cn.ds.assets.framework.common.pojo.PageResult;
import cn.ds.assets.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.ds.assets.framework.mybatis.core.mapper.BaseMapperX;
import cn.ds.assets.module.assets.dal.dataobject.dataQualityRule.BaseDataQualityRuleDO;
import org.apache.ibatis.annotations.Mapper;
import cn.ds.assets.module.assets.controller.admin.dataQualityRule.vo.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

/**
 * 数据质量规则 Mapper
 *
 * @author admin
 */
@Mapper
public interface BaseDataQualityRuleMapper extends BaseMapperX<BaseDataQualityRuleDO> {

    default PageResult<BaseDataQualityRuleDO> selectPage(BaseDataQualityRulePageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<BaseDataQualityRuleDO>()
                .likeIfPresent(BaseDataQualityRuleDO::getRuleName, reqVO.getRuleName())
                .eqIfPresent(BaseDataQualityRuleDO::getRuleDescribe, reqVO.getRuleDescribe())
                .eqIfPresent(BaseDataQualityRuleDO::getRuleContent, reqVO.getRuleContent())
                .betweenIfPresent(BaseDataQualityRuleDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(BaseDataQualityRuleDO::getRuleStatus, reqVO.getRuleStatus())
                .eqIfPresent(BaseDataQualityRuleDO::getProjectId, reqVO.getProjectId())
                .eqIfPresent(BaseDataQualityRuleDO::getTargetDb, reqVO.getTargetDb())
                .eqIfPresent(BaseDataQualityRuleDO::getTargetTable, reqVO.getTargetTable())
                .eqIfPresent(BaseDataQualityRuleDO::getTargetColumn, reqVO.getTargetColumn())
                .eqIfPresent(BaseDataQualityRuleDO::getCheckRule, reqVO.getCheckRule())
                .eqIfPresent(BaseDataQualityRuleDO::getRuleType, reqVO.getRuleType())
                .eqIfPresent(BaseDataQualityRuleDO::getQualityDimension, reqVO.getQualityDimension())
                .eqIfPresent(BaseDataQualityRuleDO::getRuleGenre, reqVO.getRuleGenre())
                .inIfPresent(BaseDataQualityRuleDO::getId,reqVO.getIds())
                .orderByDesc(BaseDataQualityRuleDO::getId));
    }

    default List<BaseDataQualityRuleDO> selectList(BaseDataQualityRuleExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<BaseDataQualityRuleDO>()
                .likeIfPresent(BaseDataQualityRuleDO::getRuleName, reqVO.getRuleName())
                .eqIfPresent(BaseDataQualityRuleDO::getRuleDescribe, reqVO.getRuleDescribe())
                .eqIfPresent(BaseDataQualityRuleDO::getRuleContent, reqVO.getRuleContent())
                .betweenIfPresent(BaseDataQualityRuleDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(BaseDataQualityRuleDO::getRuleStatus, reqVO.getRuleStatus())
                .eqIfPresent(BaseDataQualityRuleDO::getProjectId, reqVO.getProjectId())
                .eqIfPresent(BaseDataQualityRuleDO::getTargetDb, reqVO.getTargetDb())
                .eqIfPresent(BaseDataQualityRuleDO::getTargetTable, reqVO.getTargetTable())
                .eqIfPresent(BaseDataQualityRuleDO::getTargetColumn, reqVO.getTargetColumn())
                .eqIfPresent(BaseDataQualityRuleDO::getCheckRule, reqVO.getCheckRule())
                .eqIfPresent(BaseDataQualityRuleDO::getRuleType, reqVO.getRuleType())
                .eqIfPresent(BaseDataQualityRuleDO::getQualityDimension, reqVO.getQualityDimension())
                .eqIfPresent(BaseDataQualityRuleDO::getRuleGenre, reqVO.getRuleGenre())
                .inIfPresent(BaseDataQualityRuleDO::getId,reqVO.getIds())
                .orderByDesc(BaseDataQualityRuleDO::getId));
    }

    @Update("UPDATE assets_data_quality_rule SET deleted = 1, update_time = #{deletedTime} WHERE id = #{id}")
    void updateToDelete(@Param("id") Long id, @Param("deletedTime") LocalDateTime deletedTime);
}
